package com.bjpowernode;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.bjpowernode.common.client.FastDfsClient;
import com.bjpowernode.common.client.RedisClient;
import com.bjpowernode.common.consts.Keys;
import com.bjpowernode.db.mapper.BidInfoMapper;
import com.bjpowernode.db.result.BidUserMapping;
import com.bjpowernode.mvc.config.RealNameConfig;
import com.bjpowernode.mvc.config.SmsConfig;
import com.bjpowernode.task.TaskManager;
import jakarta.annotation.Resource;
import org.csource.common.MyException;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.IOException;
import java.util.List;


//@Scheduled :放在定时任务的方法上,表示方法执行定时任务,使用cron属性,表示时间和频率
//@EnableScheduling:放在启动类上,表示项目支持定时任务功能...
@EnableScheduling   //启动定时任务.
@EnableConfigurationProperties({SmsConfig.class, RealNameConfig.class})
@MapperScan(value = "com.bjpowernode.db.mapper")
@SpringBootApplication
public class YinglibaoApplication implements CommandLineRunner {


    //注入容器.
    @Resource
    private ApplicationContext applicationContext;

    /*//redis
    @Resource
    private RedisClient redisClient;*/


    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(YinglibaoApplication.class, args);

        TaskManager bean = ctx.getBean(TaskManager.class);
        bean.taskExecQuery();

    }


    @Bean
    public FastDfsClient fastDfsClient() throws MyException, IOException {
        FastDfsClient fastDfsClient = new FastDfsClient();
        fastDfsClient.init();
        return fastDfsClient;
    }


    //mybatis-plus分页拦截器,指定数据库类型
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //指定数据库类型
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


    @Override
    public void run(String... args) throws Exception {
        //在spring容器初始化完成后执行
        BidInfoMapper bidInfoMapper = applicationContext.getBean(BidInfoMapper.class);

        List<BidUserMapping> rankList = bidInfoMapper.selectBidRank();

        //将数据存放到redis
        RedisClient redisClient = applicationContext.getBean(RedisClient.class);

        //循环数据,得到排行榜
        rankList.forEach(data -> {
            redisClient.addZSet(Keys.BID_RANK, data.getPhone(), data.getBidMoney().doubleValue());
        });
    }




}
